/**
 * 
 */
package com.wolfpire.info.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.info.dao.CommunicateCooperationDao;
import com.wolfpire.info.model.CommunicateCooperation;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;

/**
 * @author lihd
 *
 */

@Repository("communicateCooperationDao")
public class CommunicateCooperationDaoImpl extends
		BaseHibernateDao<CommunicateCooperation, Long> implements
		CommunicateCooperationDao {

	@Override
	public Page<CommunicateCooperation> findCommunicateCooperations(
			Page<CommunicateCooperation> page,
			CommunicateCooperation filterCommunicateCooperation) {
		Criterion[] criterions = this.createFilter(filterCommunicateCooperation);
		int totalCount = this.findIntByCriteria(criterions);
		page.setTotalCount(totalCount);
		
		if (0 < totalCount) {
			Criteria c = this.createCriteria(criterions);
			c.addOrder(Order.desc("year"));
			c.addOrder(Order.desc("id"));
			c = this.setPageParameter(c, page);
			@SuppressWarnings("unchecked")
			List<CommunicateCooperation> result = c.list();
			page.setDataList(result);
		}
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CommunicateCooperation> list(
			CommunicateCooperation communicateCooperation) {
		Criterion[] criterions = this.createFilter(communicateCooperation);
		Criteria c = this.createCriteria(criterions);
		c.addOrder(Order.desc("year"));
		return c.list();
	}
	
	private Criterion[] createFilter(CommunicateCooperation filterCommunicateCooperation) {
		if (null == filterCommunicateCooperation) {
			return null;
		}
		List<Criterion> criterions = new ArrayList<Criterion>();
		if (null != filterCommunicateCooperation.getId()) {
			criterions.add(Restrictions.eq("id", filterCommunicateCooperation.getId()));
		}
		if (null != filterCommunicateCooperation.getYear()) {
			criterions.add(Restrictions.eq("year", filterCommunicateCooperation.getYear()));
		}
		if (null != filterCommunicateCooperation.getDelFlag()) {
			criterions.add(Restrictions.eq("delFlag", filterCommunicateCooperation.getDelFlag()));
		}
		return criterions.toArray(new Criterion[criterions.size()]);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Page<Map<String, Object>> findCCs(Page<Map<String, Object>> page,
			CommunicateCooperation filterCommunicateCooperation) {
		StringBuffer querySqlBuffer = new StringBuffer();
		StringBuffer countSqlBuffer = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		querySqlBuffer.append(" SELECT cc.*, ")
			.append(" GROUP_CONCAT(ccd.id) AS detail_ids, ")
			.append(" GROUP_CONCAT(ccd.catalog_key) AS catalog_keys, ")
			.append(" GROUP_CONCAT(ccd.project_num) AS project_nums, ")
			.append(" GROUP_CONCAT(ccd.profession_num) AS profession_nums, ")
			.append(" GROUP_CONCAT(ccd.profession_percent) AS profession_percents, ")
			.append(" GROUP_CONCAT(ccd.student_num) AS student_nums, ")
			.append(" GROUP_CONCAT(ccd.student_percent) AS student_percents, ")
			.append(" GROUP_CONCAT(ccd.course_num) AS course_nums, ")
			.append(" GROUP_CONCAT(ccd.textbook_num) AS textbook_nums, ")
			.append(" GROUP_CONCAT(ccd.twoway_student_num) AS twoway_student_nums, ")
			.append(" GROUP_CONCAT(ccd.twoway_teacher_num) AS twoway_teacher_nums, ")
			.append(" GROUP_CONCAT(ccd.expert_num) AS expert_nums, ")
			.append(" GROUP_CONCAT(ccd.certify_num) AS certify_nums ")
			.append(" FROM t_communicat_cooperation cc, t_communicatecooperation_detail ccd ")
			.append(" WHERE cc.id = ccd.communicat_cooperation_id ");
		if (null != filterCommunicateCooperation.getYear()) {
			querySqlBuffer.append(" AND cc.year_ =:year ");
			propertyNames.add("year");
			propertyValues.add(filterCommunicateCooperation.getYear());
		}
		querySqlBuffer.append(" AND cc.del_flag =:delFlag ");
		propertyNames.add("delFlag");
		propertyValues.add(Constants.NORMAL_FLAG);
		
		countSqlBuffer.append(" SELECT count(1) from (").append(querySqlBuffer).append(") AS TOTAL ");
		int totalCount = this.findSqlInt(countSqlBuffer.toString(), propertyNames, propertyValues);
		page.setTotalCount(totalCount);
		querySqlBuffer.append(" ORDER BY cc.year_ DESC, cc.id DESC");
		if (0 < totalCount) {
			List<Map<String, Object>> list = this.setPageParameter(page, querySqlBuffer.toString(), propertyNames, propertyValues);
			page.setDataList(list);
		}
		return page;
	}
}
